package com.github.niefy.modules.third.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.niefy.modules.third.dao.ThirdTemplateMsgUnreadMapper;
import com.github.niefy.modules.third.entity.ThirdTemplateMsgRead;
import com.github.niefy.modules.third.entity.ThirdTemplateMsgUnread;
import com.github.niefy.modules.third.enums.TemplateMsgReadFlagEnum;
import com.github.niefy.modules.third.service.ThirdTemplateMsgReadService;
import com.github.niefy.modules.third.service.ThirdTemplateMsgUnreadService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liuxy
 * @date 2023-4-4
 */
@Slf4j
@Service
public class ThirdTemplateMsgUnreadServiceImpl extends ServiceImpl<ThirdTemplateMsgUnreadMapper, ThirdTemplateMsgUnread> implements ThirdTemplateMsgUnreadService {
    
	@Autowired
	private WxMpService wxMpService;
	@Autowired
	private ThirdTemplateMsgReadService thirdTemplateMsgReadService;

	@Override
	public Long getUnreadMsgCount(String thirdId){
		return baseMapper.selectCount(lambdaQuery()
				.eq(ThirdTemplateMsgUnread::getThirdId, thirdId)
				.getWrapper()
		);
	}

	@Override
	public void unreadMsgToreadMsg(Long msgId){
		var thirdTemplateMsgUnread = baseMapper.selectById(msgId);
		assert BeanUtil.isNotEmpty(thirdTemplateMsgUnread):"生产环境不应出现为空的情况，测试环境为空需要补充ThirdTemplateMsgUnread表数据";
		baseMapper.deleteById(msgId);
		var thirdTemplateMsgRead = ThirdTemplateMsgRead.builder()
				.msgId(thirdTemplateMsgUnread.getMsgId())
				.thirdId(thirdTemplateMsgUnread.getThirdId())
				.openId(thirdTemplateMsgUnread.getOpenId())
				.readFlag(TemplateMsgReadFlagEnum.READ.getValue())
				.build();
		thirdTemplateMsgReadService.save(thirdTemplateMsgRead);
	}
}
